/************************************************************************
 * libspr - Ragnarok Sprite File Library                                *
 * 2012 (c) FatalEror                                                   *
 *                                                                      *
 * This program is free software, you can redistribute it and/or modify *
 * it under the terms of the GNU General Public License as published by *
 * the Free Software Foundation, either version 3 of the License, or    *
 * (at your option) any later version.                                  *
 ************************************************************************/

#include <fstream>
#include <iostream>
#include <cstdint>
#include <vector>

using namespace std;

struct SprHeader
{   //8 bytes total
	uint16_t signature;     //SPR files magic numbers, it's "SP"
	uint8_t  version_major; //Is this really SPR version?
	uint8_t  version_minor; //Is this really SPR version?
	uint16_t image_count;   //Number of image inside SPR file
	uint16_t dummy;         //Not sure what is this 2 bytes left for?
};

struct SprContent
{   //6 bytes header + ? bytes data
	uint16_t img_width;
	uint16_t img_height;
	uint16_t img_size;
	uint8_t *img_data;
};

#define CONTENT_HEADER_SIZE 6

class SprFile
{
	SprHeader m_header;
	std::vector<SprContent> m_content;
	uint32_t m_palette[256];
public:
	SprFile();
	~SprFile();
	SprFile(uint8_t *data, uint32_t size) { read(data, size); }
	bool read(uint8_t *data, uint32_t size);
#ifdef UNICODE
	SprFile(wchar_t *file_name) { read(file_name); }
	bool read(wchar_t *file_name);
#else
	SprFile(char *file_name) { read(file_name); }
	bool read(char *file_name);
#endif
	uint32_t get_version_major() { return m_header.version_major; }
	uint32_t get_version_minor() { return m_header.version_minor; }
	uint32_t get_image_count() { return m_header.image_count; }

	//Content
	uint8_t *get_content_buff(uint32_t ncontent) { return m_content.at(ncontent).img_data; }
	uint32_t get_content_size(uint32_t ncontent) { return m_content.at(ncontent).img_size; }
	uint32_t get_content_width(uint32_t ncontent) { return m_content.at(ncontent).img_width; }
	uint32_t get_content_height(uint32_t ncontent) { return m_content.at(ncontent).img_height; }

	//Palette
	uint32_t get_palette_data(uint32_t npalette) { return m_palette[npalette]; }

	//BMP function
	uint8_t *bmp_get_data(uint32_t ncontent);
	uint32_t bmp_get_size(uint32_t ncontent) { return 54 + m_content.at(ncontent).img_height*(m_content.at(ncontent).img_width*3 + (4 - (m_content.at(ncontent).img_width*3)%4)%4); }
};